package com.tlgen.utils;

import com.tlgen.bean.FastDFSFile;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

// fastDFS 工具类
public class FastDFSCient {

    private static Logger LOGGER = LoggerFactory.getLogger(FastDFSCient.class);

    static {
        try {
            String filePath = new ClassPathResource("fdfs_client.conf").getFile().getAbsolutePath();
            ClientGlobal.init(filePath);
        } catch (Exception e) {
            LOGGER.error("fastDFS client init fail!", e.getMessage());
        }
    }


    // 上传
    public static String[] upload(FastDFSFile file) {
        LOGGER.info("fileName: " + file.getName() + "; + file length: " + file.getContent().length);
        // 文件属性信息
        NameValuePair[] meta_list = new NameValuePair[1];
        meta_list[0] = new NameValuePair("author", file.getAuthor());

        long startTime = System.currentTimeMillis();
        String uploadResult[] = null;

        StorageClient storageClient = null;
        try {
            storageClient = getStorageClient();
            uploadResult = storageClient.upload_file(file.getContent(), file.getExt(), meta_list);
        } catch (Exception e) {
            LOGGER.error("上传错误! fileName: " + file.getName(), e.getMessage());
        }
        LOGGER.error("上传时间 " + (System.currentTimeMillis() - startTime) + "ms");
        if (uploadResult == null|| storageClient != null) {
            LOGGER.error("上传失败! " + storageClient.getErrorCode());
        }
        // group_name 组名 remoteFileName 完整文件名
        LOGGER.error("上传成功! group_name" + uploadResult[0] + ", remoteFileName: " + uploadResult[1]);
        return uploadResult;
    }

    // 下载
    public static InputStream downloadFile(String groupName, String remoteFileName) {
        try {
            StorageClient storageClient = getStorageClient();
            byte[] fileByte = storageClient.download_file(groupName, remoteFileName);
            InputStream ins = new ByteArrayInputStream(fileByte);
            return ins;
        } catch (Exception e) {
            LOGGER.error("上传失败!", e);
        }
        return null;
    }


    // 删除
    public static void deleteFile(String groupName, String remoteFileName) {
        try {
            StorageClient storageClient = getStorageClient();
            int i = storageClient.delete_file(groupName, remoteFileName);
            LOGGER.info("删除成功 " + i);
        } catch (Exception e) {
            LOGGER.error("删除失败!", e);
        }
    }


    // 查看文件信息
    public static FileInfo getFileInfo(String groupName, String remoteFileName) {
        try {
            StorageClient storageClient = getStorageClient();
            FileInfo file_info = storageClient.get_file_info(groupName, remoteFileName);
            return file_info;
        } catch (Exception e) {
            LOGGER.error("查看文件信息失败!", e);
        }
        return null;
    }

    // 获取文件路径
    public static String getTrackerUrl() throws IOException {
/*        TrackerClient trackerClient = new TrackerClient();
        TrackerServer trackerServer = trackerClient.getTrackerServer();
        StorageServer storeStorage = trackerClient.getStoreStorage(trackerServer);
        return "http://" + storeStorage().getInetSocketAddress().getHostString() + ":8080/";*/
        return "http://" + getTrackerServer().getInetSocketAddress().getHostName() + ":8080/";
    }

    // 生成 StorageClient 客户端
    private static StorageClient getStorageClient() throws IOException {
        TrackerServer trackerServer = getTrackerServer();
        return new StorageClient(trackerServer, null);
    }


    // 生成 TrackerServer 客户端
    private static TrackerServer getTrackerServer() throws IOException {

        TrackerClient trackerClient = new TrackerClient();
        return trackerClient.getTrackerServer();
    }
}
